package com.bana.fvs.app.module.detaile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.PageBaseParams;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.main.entity.FogZoneListItemBean;
import com.bana.fvs.app.module.detaile.entity.AreaControllerDetailBean;
import com.bana.fvs.app.module.detaile.entity.StrategieEntry;
import com.bana.fvs.app.module.detaile.entity.UpdateWorkModeResultBean;
import com.bana.fvs.app.module.detaile.entity.WorkModeItemBean;
import com.bana.fvs.app.mvp.contract.FoglightContract;
import com.bana.fvs.app.mvp.presenter.FoglightPresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wei on 2018/2/19.
 * <p>
 * 雾灯控制
 */

public class FogLightControllerActivity2 extends BaseActivity<FoglightPresenter> implements FoglightContract.IFoglightView {
    private static final String TAG = "dah_FogLightController";

    @BindView(R.id.listView)
    ListView mListView;
    @BindView(R.id.btnCommit)
    TextView mBtnCommit;

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.rb_aotu)
    RadioButton rbAotu;
    @BindView(R.id.rb_hand)
    RadioButton rbHand;
    @BindView(R.id.spinner_1)
    Spinner spinner;
    @BindView(R.id.tv_wdld)
    TextView tvWdld;
    @BindView(R.id.spinner_2)
    Spinner spinner2;
    @BindView(R.id.et_ldsc)
    EditText etLdsc;
    @BindView(R.id.et_mdsc)
    EditText etMdsc;
    @BindView(R.id.et_hdys)
    EditText etHdys;
    @BindView(R.id.et_mqsc)
    EditText etMqsc;
    @BindView(R.id.et_hsjsdsl)
    EditText etHsjsdsl;
    @BindView(R.id.et_sdsc)
    EditText etSdsc;
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rb_3)
    RadioButton rb3;
    @BindView(R.id.rb_4)
    RadioButton rb4;
    @BindView(R.id.spinner_3)
    Spinner spinner3;
    @BindView(R.id.radioGroup2)
    RadioGroup radioGroup2;
    @BindView(R.id.spinner_sbkz)
    Spinner spinnerSbkz;
    @BindView(R.id.rb_5)
    RadioButton rb5;

    private String remoteWorkModel = "0";

    private List<WorkModeItemBean> datas;
    private int switchModel = 0;
    private CommonAdapter<WorkModeItemBean> adapter;
    private CommonAdapter<WorkModeItemBean> spAdapter;
    private List<WorkModeItemBean> spDatas;
    private int choosedPosition = -1;
    private String wqbm;
    private String lightCode, gzms;
    private FogZoneListItemBean itemBean;


    public class Params extends PageBaseParams {
        public static final String PARAMS_LIGNT_CODE = "_params_lignt_code_";
        public static final String PARAMS_WQ_CODE = "_params_wq_code_";
        public static final String PARAMS_BEAN = "_params_bean_";
        public static final String PARAMS_GZMS = "_params_gzms_";
    }


    public static void startAction(Context context, String lightModel, String typeCode, FogZoneListItemBean itemBean) {
        Intent intent = new Intent(context, FogLightControllerActivity2.class);
        intent.putExtra(Params.PARAMS_LIGNT_CODE, lightModel);
        intent.putExtra(Params.PARAMS_WQ_CODE, typeCode);
        intent.putExtra(Params.PARAMS_BEAN, itemBean);
        intent.putExtra(Params.PARAMS_WQ_CODE, itemBean);
        context.startActivity(intent);
    }


    public static void startActionForResult(Activity context, String lightModel, String typeCode, FogZoneListItemBean itemBean) {
        Intent intent = new Intent(context, FogLightControllerActivity2.class);
        intent.putExtra(Params.PARAMS_LIGNT_CODE, lightModel);
        intent.putExtra(Params.PARAMS_WQ_CODE, typeCode);
        // intent.putExtra(Params.PARAMS_GZMS, gzms);
        intent.putExtra(Params.PARAMS_BEAN, itemBean);
        context.startActivityForResult(intent, 100);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_foglight_controller2;
    }

    @Override
    public void setTitle() {
        String title = getString(R.string.fogligh_controller);
        mBaseTitleBarView.setTitleText(title);
        mBaseTitleBarView.setLeftTitleGone();
        mBaseTitleBarView.setLeftDrawable(-1);
        lightCode = getIntent().getStringExtra(Params.PARAMS_LIGNT_CODE);
        remoteWorkModel = TextUtils.isEmpty(lightCode) ? "0" : lightCode;//上一页选中的
        wqbm = getIntent().getStringExtra(Params.PARAMS_WQ_CODE);
        // gzms=getIntent().getStringExtra(Params.PARAMS_GZMS);
        itemBean = (FogZoneListItemBean) getIntent().getSerializableExtra(Params.PARAMS_BEAN);
        Log.e(TAG, "remoteWorkModel========" + remoteWorkModel);


    }

    @Override
    public void initWidget() {
        etLdsc.setText(String.valueOf(itemBean.getLdsc()));
        etMdsc.setText(String.valueOf(itemBean.getMdsc()));
        etHdys.setText(String.valueOf(itemBean.getHdys()));
        etMqsc.setText(String.valueOf(itemBean.getMqys()));
        etHsjsdsl.setText(String.valueOf(itemBean.getHsjsdzsl()));
        etSdsc.setText(String.valueOf(itemBean.getSdsc()));


        mBtnCommit.setOnClickListener(this);
        mBaseTitleBarView.addOnLeftTitleClickListener(this);
        datas = new ArrayList<>();
        spDatas = new ArrayList<>();
        //获取工作模式列表
        String token = UserInfoUtils.getInstance().getToken(mContext);
        mPresenter.getFoglightMode(this, token);
        initData();
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            choosedPosition = position;
            datas.get(position).setChoosed(!datas.get(position).isChoosed());
            adapter.notifyDataSetChanged();
        });

        initSpinner();

        radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        setMode1();
                        break;
                    case R.id.rb_2:
                        setMode2();
                        break;
                    case R.id.rb_3:
                        setMode3();
                        break;
                    case R.id.rb_4:
                        setMode4();
                        break;
                    case R.id.rb_5:
                        setMode5();
                        break;
                }
            }
        });

    }

    private void setListViewData() {
        if (null != datas && !datas.isEmpty()) {
            for (int i = 0; i < datas.size(); i++) {
                if (datas.get(i).getDm().equals(lightCode)) {
                    choosedPosition = i;
                }
            }

            spinnerSbkz.setSelection(choosedPosition + 1);


            adapter = new CommonAdapter<WorkModeItemBean>(datas, R.layout.item_layout_switch_button_item) {
                @Override
                public void convert(ViewHolder holder, WorkModeItemBean bean) {
                    int position = holder.getPosition();
                    holder.setText(R.id.tvName, bean.getDmsm());
                    boolean isChoosed = choosedPosition == position;
                    bean.setChoosed(isChoosed);
                    holder.setImageResource(R.id.ivChoose, isChoosed ? R.drawable.radio_checked : R.drawable.radio_unchecked);
                }
            };
            mListView.setAdapter(adapter);
        }


    }

    private boolean isHalf = false;

    private void setSpinnerData() {

        spAdapter = new CommonAdapter<WorkModeItemBean>(spDatas, R.layout.item_layout_spinner_item) {
            @Override
            public void convert(ViewHolder helper, WorkModeItemBean item) {

                helper.setText(R.id.tvName, item.getDm());
                if (isHalf) {
                    if (helper.getPosition() > 4) {
                        helper.getView(R.id.rl).setVisibility(View.GONE);
                    }else{
                        helper.getView(R.id.rl).setVisibility(View.VISIBLE);
                    }
                } else {
                    helper.getView(R.id.rl).setVisibility(View.VISIBLE);
                }

            }
        };
        spinner.setAdapter(spAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                tvWdld.setText(spDatas.get(position).getDmsm());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        for (int i = 0; i < spDatas.size(); i++) {
            if (spDatas.get(i).getDm().equals(itemBean.getWdlddj() + "")) {
                spinner.setSelection(i);
                break;
            }
        }


    }


    private static final String DEBUG_TAG = "debug_FogLightControllerActiv";

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.btnCommit:
                if (spinnerSbkz.getSelectedItemPosition() == 0) {
                    ToastUtil.showShort(mContext,"请选择雾灯闪烁状态");
                    return;
                }
               // WorkModeItemBean workModeItemBean = datas.get(choosedPosition);
                String token = UserInfoUtils.getInstance().getToken(mContext);

//                "wqbm":27,
//                    "gzms":1,
//                    "wdkz":1,
//                    "ldsc":250,
//                    "mdsc":500,
//                    "hdys":1,
//                    "mqys":2,
//                    "hsjsdzsl":3,
//                    "njddj":1,
//                    "sfds":1,
//                    "sdsc":5


//                    "wdld":25,
//                    "kzcl":1,
//                    "sspl":1,
//                    "zkb":1

                HashMap<String, Object> data = new HashMap<>();
                data.put("wqbm", itemBean.getWqbm());
                data.put("gzms", "2");
                data.put("wdkz", spinnerSbkz.getSelectedItemPosition());
                data.put("ldsc", etLdsc.getText().toString());
                data.put("mdsc", etMdsc.getText().toString());
                data.put("hdys", etHdys.getText().toString());

                data.put("wdld", itemBean.getWdld());
                data.put("kzcl", itemBean.getKzcl());
                data.put("sspl", itemBean.getSspl());
                data.put("zkb", itemBean.getZkb());

                if (Integer.parseInt(etMqsc.getText().toString()) > 99) {
                    ToastUtil.showShort(mContext, "红色延时时间不超过99秒");
                    return;
                }
                data.put("mqys", etMqsc.getText().toString());
                if (etHsjsdsl.getVisibility() == View.VISIBLE)
                    data.put("hsjsdzsl", etHsjsdsl.getText().toString());
                else
                    data.put("hsjsdzsl", spinner3.getSelectedItem().toString());
                data.put("njddj", itemBean.getNjddj());
                if (itemBean.isSfds())
                    data.put("sfds", 1);
                else
                    data.put("sfds", 0);
                data.put("sdsc", etSdsc.getText().toString());
                data.put("wdld", tvWdld.getText().toString());
                data.put("wdlddj", spDatas.get(spinner.getSelectedItemPosition()).getDm());
                //雾区编号 dmlb 工作模式 dm
                mPresenter.http_foglightWorkModeUpdate(mActivity, token, data);
                break;
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(Object o) {
    }


    @Override
    public void getFogLightMode(List<WorkModeItemBean> workMeItemBeanList) {
        Log.e(TAG, "getFogLightMode: workMeItemBeanList===" + workMeItemBeanList);
        if (null != workMeItemBeanList && !workMeItemBeanList.isEmpty()) {
            spDatas.clear();

            spDatas.addAll(workMeItemBeanList);
            setSpinnerData();
        }
    }

    @Override
    public void getFogLightModeError(CommException e) {

    }

    private void initData() {
        datas.add(new WorkModeItemBean("1", "强制闪红灯"));
        datas.add(new WorkModeItemBean("2", "强制闪黄灯"));
        datas.add(new WorkModeItemBean("3", "检测来车红闪"));
        datas.add(new WorkModeItemBean("4", "关闭"));
        setListViewData();
    }


    private void setMode1() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        etLdsc.setText("1500");
        etMdsc.setText("0");
        etMqsc.setText("2");
        etHdys.setText("15");

        spinner2.setEnabled(false);
        spinner2.setSelection(0);

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
//        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(2);
        isHalf = true;
        spAdapter.notifyDataSetChanged();
        spinner.setSelection(1);

    }

    private void setMode2() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        initSpinner();
        spinner2.setSelection(2);
        etLdsc.setText("250");
        etMdsc.setText("750");
        etMqsc.setText("2");
        etHdys.setText("15");

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
//        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(2);

        isHalf = false;
        spAdapter.notifyDataSetChanged();
        spinner.setSelection(1);
    }

    private void setMode3() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(true);
        etHsjsdsl.setEnabled(false);


        initSpinner();
        spinner2.setSelection(1);

        etLdsc.setText("400");
        etMdsc.setText("1600");
        etMqsc.setText("2");
        etHdys.setText("1");

        etHsjsdsl.setVisibility(View.GONE);
        spinner3.setVisibility(View.VISIBLE);
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    etHdys.setEnabled(true);
                } else if (position < 4) {
                    etHdys.setEnabled(false);
                    etHdys.setText("15");
                } else {
                    etHdys.setEnabled(false);
                    etHdys.setText("30");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner3.setSelection(0);

        //        choosedPosition = 2;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(3);

        isHalf = false;
        spAdapter.notifyDataSetChanged();
        spinner.setSelection(1);
    }

    private void setMode4() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.sspl2,
                android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);

        spinner2.setEnabled(true);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        etLdsc.setText("1500");
                        etMdsc.setText("0");
                        isHalf = true;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 1:
                        etLdsc.setText("200");
                        etMdsc.setText("800");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 2:
                        etLdsc.setText("125");
                        etMdsc.setText("375");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 3:
                        etLdsc.setText("83");
                        etMdsc.setText("167");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ;

        isHalf = true;
        spAdapter.notifyDataSetChanged();
        spinner.setSelection(2);
        spinner2.setSelection(0);


        etLdsc.setText("1500");
        etMdsc.setText("0");
        etMqsc.setText("2");
        etHdys.setText("15");

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
        //        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(1);
    }


    private void setMode5() {
        etMdsc.setEnabled(true);
        etLdsc.setEnabled(true);
        etMqsc.setEnabled(true);
        etHdys.setEnabled(true);
        etHsjsdsl.setEnabled(true);
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
        initSpinner();
        isHalf = false;
        spAdapter.notifyDataSetChanged();
    }

    private void initSpinner() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.sspl,
                android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);

        spinner2.setEnabled(true);


        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        etLdsc.setText("400");
                        etMdsc.setText("1600");
                        break;
                    case 2:
                        etLdsc.setText("250");
                        etMdsc.setText("750");
                        break;
                    case 3:
                        etLdsc.setText("167");
                        etMdsc.setText("333");
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ;

    }

    @Override
    public void updtaeFogLightMode(ArrayList<UpdateWorkModeResultBean.Item2Bean> resultBean) {
       
            if (resultBean != null) {
                ToastUtil.showShort(mContext, "操作成功");
//                Intent intent=new Intent();
//                intent.putExtra(ConstantsField.ITEM,resultBean.getFogArea());
//                setResult(100,intent);
                finish();
            } else {
                ToastUtil.showShort(mContext,"操作失败，请联系系统管理员！");
            }

    }

    @Override
    public void getStrategiesResult(ArrayList<StrategieEntry> strategies) {
        
    }

    @Override
    public void getControllersResult(ArrayList<AreaControllerDetailBean> strategies) {

    }

    @Override
    public void getStrategiesError(CommException e) {

    }
}
